#!/usr/bin/python
# -*- coding: utf-8 -*-  
import re  
import urllib  
import urllib2  
import cookielib  
from bs4 import BeautifulSoup
from mypost import *

#!/usr/bin/env python

import urllib
import httplib2

http = httplib2.Http()

path = 'http://127.0.0.1:8080/drupal/?q=node&destination=node'
form_data =  {'name': 'qing', 'pass': 'qing2drupal', 'form_id':'user_login_block'}  
headers = {'Content-type': 'application/x-www-form-urlencoded'}
headers.update({'Connection': 'keep-alive'})   
response, content = http.request(path, 'POST', headers=headers, body=urllib.urlencode(form_data))
headers.update({'Cookie': response['set-cookie']})   ###将获得cookie设置到请求头中，以备下次请求使用
print headers

#response, content = http.request(path, 'GET', headers=headers)  ##本次请求就不用带用户名，密码了

  
##模拟登录  
#cj = cookielib.CookieJar()  
##用户名和密码
##form_data =  {'name': 'qing', 'pass': 'qing@drupal', 'op':'登录'}  
#form_data =  {'name': 'qing', 'pass': 'qing2drupal', 'form_id':'user_login_block'}  
#post_data = urllib.urlencode(form_data)
##登录路径  
#path = 'http://127.0.0.1:8080/drupal/?q=node&destination=node'
#opener = urllib2.build_opener(urllib2.HTTPCookieProcessor(cj))  
#opener.addheaders = [('User-agent', 'Opera 9.2'), ('Accept', 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8'),
#('Accept-Language','zh-cn,zh;q=0.8,en-us;q=0.5,en;q=0.3'),
#('Accept-Encoding','gzip, deflate'),
#('Connection','keep-alive')]
#urllib2.install_opener(opener) 
#req = urllib2.Request(path, post_data)  
#conn = urllib2.urlopen(req)
#
article_data = {'title':'dddd', 
'field_tags[und]':'tags', 
'body[und][0][summary]':'',
'body[und][0][value]':'body0',
'body[und][0][format]':'filtered_html',
'field_image[und][0][fid]':'0',
'field_image[und][0][display]':'1',
'changed':'',
'form_build_id':'',
'form_token':'',
'form_id':'article_node_form',
'menu[link_title]':'',
'menu[description]':'',
'menu[parent]':'main-menu:0',
'menu[weight]':'0',
'log':'',
'path[pathauto]':'1',
'comment':'2',
'name':'qing',
'date':'',
'status':'1',
'promote':'1',
'additional_settings_active_tab':'edit-options',
'op':'保存'
}

add_path = 'http://127.0.0.1:8080/drupal/node/add/article'
#add_path = 'http://127.0.0.1:8080/drupal/admin/people'
response, content = http.request(add_path, 'GET', headers=headers)
#print headers
soup = BeautifulSoup(content)
#for inputs in soup.findAll('input'):
#    print inputs
form_token = soup.find('input',attrs={'name':'form_token'})['value']
form_build_id = soup.find('input',attrs={'name':'form_build_id'})['value']
article_data.update({'form_token':form_token})
article_data.update({'form_build_id':form_build_id})

#add_path = 'http://127.0.0.1:8080/drupal/admin/people'
files = {'files[field_image_und_0]':''}
content_type, body = encode_multipart_formdata(article_data, files)
article_data.update({'content-type': content_type})
article_data.update({'content-length': str(len(body))})
print article_data
#content = posturl(add_path, headers, article_data, files)
#response, content = http.request(add_path, 'POST', headers, body)
response, content = http.request(add_path, 'POST', body,headers)
print response
print content
